<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>房间管理</el-breadcrumb-item>
      <el-breadcrumb-item>房态管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-table :data="typeList" border style="width: 100%" stripe>
        <el-table-column prop="id" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="typename" label="房型" width="180">
        </el-table-column>
        <el-table-column prop="typedec" label="描述">
        </el-table-column>
        <el-table-column prop="price" label="价格">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="success" icon="el-icon-plus" circle @click="addDialogVisible = true"></el-button>
            <el-button type="primary" icon="el-icon-edit" circle @click="getTypeById(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="deleteType(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>



    </el-card>
    <!-- 新增房型对话框 -->
    <el-dialog
      title="修改房态"
      :visible.sync="addDialogVisible"
      width="60%"
      :before-close="handleClose">
      <el-form ref="addTypeForm" :model="addTypeForm" label-width="80px">
        <el-form-item label="房型">
            <el-input v-model="addTypeForm.typename"></el-input>
        </el-form-item>
        <el-form-item label="描述">
            <el-input v-model="addTypeForm.typedec"></el-input>
        </el-form-item>
        <el-form-item label="价格">
            <el-input v-model="addTypeForm.price"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addType()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改对话框 -->
    <el-dialog
      title="新增房型"
      :visible.sync="updateDialogVisible"
      width="60%"
      :before-close="handleClose">
      <el-form ref="updateTypeForm" :model="updateTypeForm" label-width="80px">
        <el-form-item label="房型">
            <el-input v-model="updateTypeForm.typename"></el-input>
        </el-form-item>
        <el-form-item label="描述">
            <el-input v-model="updateTypeForm.typedec"></el-input>
        </el-form-item>
        <el-form-item label="价格">
            <el-input v-model="updateTypeForm.price"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateType()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'Room',
    data() {
      return {
        //获取type表格
        typeList: [],
        //控制增加对话框是否关闭
        addDialogVisible: false,
        updateDialogVisible: false,
        addTypeForm: {
          id: '',
          typename: '',
          typedec: '',
          price: ''
        },
        updateTypeForm: {
          typename: '',
          typedec: '',
          price: ''
        }
      }
    },
    created() {
      this.getTypeList()
    },
    methods: {
      handleClose(done) {
              this.$confirm('确认关闭？')
                .then(_ => {
                  done();
                })
                .catch(_ => {});
      },

      getTypeList() {
        const that = this
        this.axios.get('http://localhost:8090/type/getTypeList').then(result => {
          this.typeList = result.data
        })
      },
      addType(){
        const that = this
        this.axios.post('http://localhost:8090/type/addType', this.addTypeForm)
        this.$message.success("新增成功")
        this.addDialogVisible = false
        this.getTypeList()
      },
      getTypeById(id){
        this.updateDialogVisible = true
        const that = this
        this.axios.get('http://localhost:8090/type/getTypeById/'+id).then(result => {
           this.updateTypeForm = result.data
        })
      },
      updateType(){
        this.updateDialogVisible = false
        const that = this
        this.axios.post('http://localhost:8090/type/updateType',this.updateTypeForm)
        this.getTypeList()
        this.$message.success("修改成功")
      },
      deleteType(id){
        const that = this
        this.axios.delete('http://localhost:8090/type/deleteType/'+id)
        this.getTypeList()
        this.$message.success("删除成功")
      }



    }
  }
</script>

<style>
</style>
